package sk.tuke.magsa.personalistika.ui;

import sk.tuke.magsa.framework.Utilities;
import sk.tuke.magsa.framework.ValidatorException;
import sk.tuke.magsa.framework.ui.FormDialog;
import sk.tuke.magsa.personalistika.entity.*;
import sk.tuke.magsa.personalistika.Application;

public class ZamestnanecForm extends FormDialog<Zamestnanec>{
    public ZamestnanecForm() {
        super(new Zamestnanec());
    }

    public ZamestnanecForm(Zamestnanec entity) {
        super(entity);
    }

    public Zamestnanec show() {
        String input;
        
        
        String meno = entity.getMeno();
        do
        {
            try
            {
                System.out.print(String.format("Enter meno [%s]: ", entity.getMeno()));
                input = Utilities.readLine();
                if(!"".equals(input))
                {
                    meno = input;
                }
            
//-------------------------------Required-------------------------------
        if(meno == null) {
             throw new ValidatorException("Property 'Meno' is required!");
            }
            
//-------------------------------LENGTH-------------------------------
        if(meno != null) {
            if( meno.length() > 30 || meno.length() < 2) {
                throw new ValidatorException("Property 'Meno' has length out of range!");
            }
        }
                break;
            }
            catch(NumberFormatException e)
            {
                System.out.println("Cannot parse the entered value!");
            }
            catch(ValidatorException e)
            {
                System.out.println(e.getMessage());
            }
        }
        while(true);
        entity.setMeno(meno);
        
        String priezvisko = entity.getPriezvisko();
        do
        {
            try
            {
                System.out.print(String.format("Enter priezvisko [%s]: ", entity.getPriezvisko()));
                input = Utilities.readLine();
                if(!"".equals(input))
                {
                    priezvisko = input;
                }
            
//-------------------------------Required-------------------------------
        if(priezvisko == null) {
             throw new ValidatorException("Property 'Priezvisko' is required!");
            }
            
//-------------------------------LENGTH-------------------------------
        if(priezvisko != null) {
            if( priezvisko.length() > 30 || priezvisko.length() < 2) {
                throw new ValidatorException("Property 'Priezvisko' has length out of range!");
            }
        }
                break;
            }
            catch(NumberFormatException e)
            {
                System.out.println("Cannot parse the entered value!");
            }
            catch(ValidatorException e)
            {
                System.out.println(e.getMessage());
            }
        }
        while(true);
        entity.setPriezvisko(priezvisko);
        
        Integer vek = entity.getVek();
        do
        {
            try
            {
                System.out.print(String.format("Enter vek [%s]: ", entity.getVek()));
                input = Utilities.readLine();
                if(!"".equals(input))
                {
                    vek = Integer.valueOf(input);
                }
                break;
            }
            catch(NumberFormatException e)
            {
                System.out.println("Cannot parse the entered value!");
            }
            catch(ValidatorException e)
            {
                System.out.println(e.getMessage());
            }
        }
        while(true);
        entity.setVek(vek);





//"tl" = 9
//"tn" = Oddelenie
//"entity" = entity Oddelenie [nazov : STRING, kod : STRING, uroven : REAL]
//"rProperty" = entity Oddelenie [nazov : STRING, kod : STRING, uroven : REAL]
//"property" = nazov : STRING
//"oName" = entityNazov
//"rName" = OddelenieNazov
        Oddelenie entityNazov = new Oddelenie();
        String oddelenieNazov = entityNazov.getNazov();
        do
        {
            try
            {
                System.out.print("Enter oddelenieNazov [" + entityNazov.getNazov() + "]: ");
                input = Utilities.readLine();
                if (!"".equals(input)) 
                {
                    oddelenieNazov = input;
                }
                entityNazov.setNazov(oddelenieNazov);

                
//-------------------------------Required-------------------------------
        if(oddelenieNazov == null) {
             throw new ValidatorException("Property 'Nazov' is required!");
            }
                
//-------------------------------LENGTH-------------------------------
        if(oddelenieNazov != null) {
            if( oddelenieNazov.length() > 30 || oddelenieNazov.length() < 5) {
                throw new ValidatorException("Property 'Nazov' has length out of range!");
            }
        }
            if(Application.getInstance().getOddelenieDao().findByProperty(oddelenieNazov, 0).isEmpty())
            {
                throw new ValidatorException("There is no item with entered name. Try again.");
            }

            break;
            }
             catch (NumberFormatException e) 
            {
                System.out.println("Cannot parse the entered value!");
            } 
            catch (ValidatorException e) 
            {
                System.out.println(e.getMessage());
            }
        }
        while(true);
        entity.setOddelenie(Application.getInstance().getOddelenieDao().findByProperty(oddelenieNazov, 0).get(0).getIdent());



        return entity;
    }
}
